package homework;

import java.lang.reflect.Field;

/*
写一个方法，此方法可以获取obj对象中名为propertyName的属性的值
public Object getProperty(Object obj, String propertyName){ }
*/

public class Test5 {
    public static void main(String[] args) throws Exception {
        Demo d =new Demo();
        setProperty(d,"s","我在测试");
        System.out.println(d.s);
    }

    public static void setProperty(Object obj,String propertyName,Object value) throws Exception{
        //获取obj字节码
        Class cls =obj.getClass();
        //得到propertyName字段
        Field field =cls.getDeclaredField(propertyName);

        field.setAccessible(true);
        field.set(obj, value);
    }
}

